<?php
global $_W, $_GPC;
load()->func('tpl');
$uniacid=intval($_W['uniacid']);
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$opp = 'adminmail';
if ($operation == 'display') {
  $pindex = max(1, intval($_GPC['page']));
  $psize = 10;
  $list = pdo_fetchall("SELECT a.* FROM " . tablename('bbyscm_card_mail') . " as a WHERE a.uniacid = $uniacid $condition ORDER BY a.createtime DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
  $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('bbyscm_card_mail') . " as a WHERE a.uniacid = $uniacid  $condition");
  $pager = pagination($total, $pindex, $psize);

  if (!empty($list)) {
    foreach ($list as $key => $card) {
      $list[$key]['wxacode_b'] = tomedia($card['wxacode_b']);
      $imgs = iunserializer($card['imgs']);
      foreach ($imgs as $k => $imgid) {
        $imgs[$k] = tomedia($imgid);
      }
      $list[$key]['imgs'] = $imgs;
    }
  }
} elseif ($operation == 'post') {
  $id = intval($_GPC['id']);
  if (checksubmit('submit')) {
    $data = array(
      'uniacid' => $_W['uniacid'],
      'mail_name' => $_GPC['mail_name'],
      'admin_name' => $_GPC['admin_name'],
      'admin_cardid' => $_GPC['admin_cardid'],
      'admin_openid' => $_GPC['admin_openid'],
      // 'mail_key' => $_GPC['mail_key'],
      'mail_key' => 1,
      'start_time'=> strtotime($_GPC['activity']['start']),
      'end_time'=> strtotime($_GPC['activity']['end']),
      'createtime' => time(),
    );
    /*		$data2 = array(
          'uniacid' => $_W['uniacid'],
          'mails_id' => $id,
          'admin_cardid' => $_GPC['admin_cardid'],
          'admin_openid' => $_GPC['admin_openid'],
          'mail_member' => $_GPC['admin_cardid'],
          'member_openid' => $_GPC['admin_openid'],
          'mail_key' => 1,
    ); */
    if (!empty($id)) {
      pdo_update('bbyscm_card_mail', $data, array('id' => $id));
      //				pdo_update('bbyscm_card_mails', $data2, array('mails_id' => $id));
    } else {
      pdo_insert('bbyscm_card_mail', $data);
      $id = pdo_insertid();
    }
    message('更新设置成功！', $this->createWebUrl('adminmail', array('op' => 'display')), 'success');
  }
  $adminmail = pdo_fetch("select * from " . tablename('bbyscm_card_mail') . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
} elseif ($operation == 'delete') {
  $id = intval($_GPC['id']);
  $adminmail = pdo_fetch("SELECT id FROM " . tablename('bbyscm_card_mail') . " WHERE id = '$id' AND uniacid=" . $_W['uniacid'] . "");
  if (empty($adminmail)) {
    message('抱歉，记录不存在或是已经被删除！', $this->createWebUrl('adminmail', array('op' => 'display')), 'error');
  }
  pdo_delete('bbyscm_card_mail', array('id' => $id));
  message('通讯录删除成功！', $this->createWebUrl('adminmail', array('op' => 'display')), 'success');
} elseif ($operation == 'detail') {
  $openid = $_W["openid"];
  $mm_key = intval($_GPC["id"]);
  if ($mm_key > 0) {
    // $where .= "WHERE `uniacid`= uniacid and '{$mm_key}' = mails_id and '1'= mail_key";
    $where .= " uniacid= {$uniacid} and ai_card = {$mm_key}";
  }
  $pindex = max(1, intval($_GPC['page']));
  $psize = 10;

  $sql = "SELECT * from " . tablename("bbyscm_card") . "  where {$where}";

  $list = pdo_fetchall($sql);
  // echo $sql;
  // print_r($list);
  // die('ddd');

  //	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('bbyscm_card_mails') . " as a WHERE a.uniacid = $uniacid ");

  //	  $pager = pagination($list, $pindex, $psize);

  if (count($list) > 0) {
    foreach ($list as $key => $value) {
      $list[$key]["QR_code"] = tomedia($value["QR_code"]);
      $list[$key]["avater"] = tomedia($value["avater"]);
    }
    //	return $this->result(0, $sql, $list);
  } else {
    return $this->result(1, '', 0);
  }
}else {
  message('请求方式不存在');
} 

include $this->template('web/adminmail');

